import axios, { type AxiosResponse, type AxiosRequestConfig, type AxiosRequestHeaders, type InternalAxiosRequestConfig } from 'axios';
import { Toast } from 'tdesign-mobile-vue';
import { useLoadingStore } from '@/stores/loading';

let loadingStore = useLoadingStore()


interface ResponseData<T> {
	code: number;
	data: T;
	message: string;
}


const service = axios.create({
	baseURL: import.meta.env.VITE_API_FULL_URL,
	// baseURL: 'http://1.92.111.116:9527',
	// baseURL: 'https://sam.smartiesconsulva.com',
	// timeout: 50000,
});


service.interceptors.request.use(
	(config: InternalAxiosRequestConfig<any>) => {

		(config.headers as AxiosRequestHeaders).disturbedUrl = '0';
		(config.headers as AxiosRequestHeaders).disturbedPar = '1';
		(config.headers as AxiosRequestHeaders).encrypted = '0';
		(config.headers as AxiosRequestHeaders).ownerShip = import.meta.env.VITE_OWNERSHIP;
		// (config.headers as AxiosRequestHeaders).ownerShip = 'cashline_ios';
		const userInfoStore = { token: '' }
		if (userInfoStore.token) {
			(config.headers as AxiosRequestHeaders).token = userInfoStore.token
		} else {

		}
		let blackBook = ['/DQjSJRd/nIQk/aTXdyJPd/vxHuX','/DQjSJRd/nIQk/aTXdyJPd/wjWTRWGiUwy']
		if(!blackBook.includes(config.url!)){
			loadingStore.showLoading(true)
		}
		// if (config.url != '/DQjSJRd/nIQk/aTXdyJPd/vxHuX' && config.url != '/DQjSJRd/nIQk/aTXdyJPd/wjWTRWGiUwy') {
		// 	loadingStore.showLoading(true)
		// }


		return config;
	}
);


service.interceptors.response.use(
	
	async (response: AxiosResponse<ResponseData<any>>) => {

		let blackBook = ['/hrktdowmr/cOMpChS/ABdSCJ/sm8YTnDzx']
		if(!blackBook.includes(response.config.url!)){
			loadingStore.showLoading(false)
		}
		
		// loadingStore.showLoading(false)

	

		// 	
		if (response.data.code == -1) {
			window.location.href = '/' 
			Toast({
				message: response.data.message,
				duration: 3 * 1000
			})
			
		}
		
		return response;
		

	},
	(error) => {
		
		loadingStore.showLoading(false)
		return Promise.reject(error);
	}
);

export default service;
